<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="学号">
              <a-input v-model="queryParam.xh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="导师姓名">
                <a-input v-model="queryParam.dsxm" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学生类别">
                <a-select :allowClear="true" v-model="queryParam.xslbm">
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="培养模式">
                <a-select :allowClear="true" v-model="queryParam.pymsm">
                  <a-select-option v-for="i in TYPE0347" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="院系">
                <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.ssyxm" @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="专业">
                <a-select :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.sszym">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学籍状态">
                <a-select allowClear v-model="queryParam.xjztm" placeholder="请选择">
                  <a-select-option v-for="i in TYPE0073" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="8" :sm="16">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <!-- <a-button type="primary" @click="exportExcel">导出</a-button> -->
              <a-button style="margin-left: 8px" @click="() => queryParam = {ssyxm: selectMenu.yx.length === 1 ? queryParam.ssyxm : ''}">重置</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      v-if="initDone"
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :pageSize="parameter.pageSize"
      :pageNum="parameter.pageNum"
      :scroll="{x: 1600}"
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
      </span>

      <template slot="xh" slot-scope="a,b">
         <a @click="direct(b)">{{a}}</a>
      </template>
      <template slot="operation" slot-scope="a,b">
        <a @click="openEdit(b)">调换导师</a>
      </template>
    </s-table>

    <set-teacher
      ref="setTeacher"
      :visible.sync="visible"
      :info.sync="stuInfo"
      @close="visible = false"
      @refresh="refresh"
    />
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'
import setTeacher from './setTeacher'

export default {
  name: 'TableList',
  components: {
    setTeacher,
    STable
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      // 查询参数
      queryParam: {
        xjztm: ''
      },
      postParams: {},
      stuInfo: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '学号',
          dataIndex: 'xh',
          scopedSlots: { customRender: 'xh' }
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '专业',
          dataIndex: 'sszymc'
        },
        {
          title: '年级',
          dataIndex: 'sznj'
        },
        {
          title: '导师姓名',
          dataIndex: 'dsxm'
        },
        {
          title: '第二导师姓名',
          dataIndex: 'dedsxm'
        },
        // {
        //   title: '证件号码',
        //   dataIndex: 'zjhm'
        // },
        {
          title: '学生类别',
          dataIndex: 'xslbmc'
        },
        {
          title: '学籍状态',
          dataIndex: 'xjztmc'
        },
        {
          title: '操作',
          fixed: 'right',
          scopedSlots: { customRender: 'operation' },
          width: '150px'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.student.stuInfoList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true, // 是否显示alert，已选中
      visible: false,
      confirmLoading: false,
      editId: null,
      selectMenu: {
        yx: [], // 院系
        zy: [] // 所属专业
      },
      initDone: false
    }
  },
  computed: {
    ...mapState({
      TYPE0347: state => state.app['TYPE0347'], // 培养模式
      TYPE0075: state => state.app['TYPE0075'], // 学生类别
      TYPE0073: state => state.app['TYPE0073'] // 学籍状态
    })
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      let pageParams = vm.$store.state.app.pageParams || {}
      if (from.path === '/student/stuStatus/search/stuView' && pageParams.path === vm.$route.path) {
        vm.queryParam = pageParams.params
        vm.parameter = pageParams.pagination
      }
      vm.initSelectMenu()
    })
  },
  beforeRouteLeave (to, from, next) {
    if (to.path === '/student/stuStatus/search/stuView') {
      this.$store.dispatch('app/setPageParams', {
        path: this.$route.path,
        params: this.queryParam,
        pagination: this.parameter
      })
    }
    next()
  },
  created () {
    this.initSelectMenu()
    this.$store.dispatch('app/setDictionary', 'TYPE0073')
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0347')
  },
  // watch: {
  //   'visible': function (newVal, oldVal) {
  //     if (!newVal) {
  //       this.$refs.table.refresh(true)
  //     }
  //   }
  // },
  methods: {
    refresh() {
      this.$refs.table.refresh(false)
    },
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'ssyxm', this.selectMenu.yx[0].dwh)
          this.yxChange(this.queryParam.ssyxm)
        }
        this.initDone = true
      })
    },
    direct (data) {
      this.$router.push(`/student/stuStatus/search/stuView?xh=${data.xh}`)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    openEdit (stu) {
      this.$api.student.stuInfo(stu.xh).then(res => {
        this.stuInfo = {...stu, ...res.data}
      })
      this.visible = true
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    exportExcel () {
      this.$export('/xn/xsxx/exportXjxx', {
        ...this.queryParam
      })
    }
  }
}
</script>
